* This is the do file to create "Table C1. Heterogeneity across Baseline Abilites (Quantile)"
set seed 123

use "$path_data/temp/followup_student_parents_matched", clear


xtile DT_score_pre_std_quartile = DT_score_pre_std , nq(4)
gen DT_score_pre_std_q1 = 1 if DT_score_pre_std_quartile == 1
gen DT_score_pre_std_q2 = 1 if DT_score_pre_std_quartile == 2
gen DT_score_pre_std_q3 = 1 if DT_score_pre_std_quartile == 3
gen DT_score_pre_std_q4 = 1 if DT_score_pre_std_quartile == 4
recode DT_score_pre_std_q* (.=0)

xtile rosen_pre_std_quartile = rosen_pre_std , nq(4)
gen rosen_pre_std_q1 = 1 if rosen_pre_std_quartile == 1
gen rosen_pre_std_q2 = 1 if rosen_pre_std_quartile == 2
gen rosen_pre_std_q3 = 1 if rosen_pre_std_quartile == 3
gen rosen_pre_std_q4 = 1 if rosen_pre_std_quartile == 4
recode rosen_pre_std_q* (.=0)

xtile cpcs_pre_std_quartile = cpcs_pre_std , nq(4)
gen cpcs_pre_std_q1 = 1 if cpcs_pre_std_quartile == 1
gen cpcs_pre_std_q2 = 1 if cpcs_pre_std_quartile == 2
gen cpcs_pre_std_q3 = 1 if cpcs_pre_std_quartile == 3
gen cpcs_pre_std_q4 = 1 if cpcs_pre_std_quartile == 4
recode cpcs_pre_std_q* (.=0)


/// Cognitive
wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_q1 == 1, cluster(school_no) reps(1000)
scalar n_cog_q1 = e(N)
scalar n_clust_cog_q1 = e(N_clust)
matrix cog_q1 = r(table)

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_q2 == 1, cluster(school_no) reps(1000)
scalar n_cog_q2 = e(N)
scalar n_clust_cog_q2 = e(N_clust)
matrix cog_q2 = r(table)

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_q3 == 1, cluster(school_no) reps(1000)
scalar n_cog_q3 = e(N)
scalar n_clust_cog_q3 = e(N_clust)
matrix cog_q3 = r(table)

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_q4 == 1, cluster(school_no) reps(1000)
scalar n_cog_q4 = e(N)
scalar n_clust_cog_q4 = e(N_clust)
matrix cog_q4 = r(table)

/// RSES
wildbootstrap reg RSES_std treatment if rosen_pre_std_q1 == 1, cluster(school_no) reps(1000)
scalar n_rses_q1 = e(N)
scalar n_clust_rses_q1 = e(N_clust)
matrix rses_q1 = r(table)

wildbootstrap reg RSES_std treatment if rosen_pre_std_q2 == 1, cluster(school_no) reps(1000)
scalar n_rses_q2 = e(N)
scalar n_clust_rses_q2 = e(N_clust)
matrix rses_q2 = r(table)

wildbootstrap reg RSES_std treatment if rosen_pre_std_q3 == 1, cluster(school_no) reps(1000)
scalar n_rses_q3 = e(N)
scalar n_clust_rses_q3 = e(N_clust)
matrix rses_q3 = r(table)

wildbootstrap reg RSES_std treatment if rosen_pre_std_q4 == 1, cluster(school_no) reps(1000)
scalar n_rses_q4 = e(N)
scalar n_clust_rses_q4 = e(N_clust)
matrix rses_q4 = r(table)

/// CPCS
wildbootstrap reg CPCS_std treatment if cpcs_pre_std_q1 == 1, cluster(school_no) reps(1000)
scalar n_cpcs_q1 = e(N)
scalar n_clust_cpcs_q1 = e(N_clust)
matrix cpcs_q1 = r(table)

wildbootstrap reg CPCS_std treatment if cpcs_pre_std_q2 == 1, cluster(school_no) reps(1000)
scalar n_cpcs_q2 = e(N)
scalar n_clust_cpcs_q2 = e(N_clust)
matrix cpcs_q2 = r(table)

wildbootstrap reg CPCS_std treatment if cpcs_pre_std_q3 == 1, cluster(school_no) reps(1000)
scalar n_cpcs_q3 = e(N)
scalar n_clust_cpcs_q3 = e(N_clust)
matrix cpcs_q3 = r(table)

wildbootstrap reg CPCS_std treatment if cpcs_pre_std_q4 == 1, cluster(school_no) reps(1000)
scalar n_cpcs_q4 = e(N)
scalar n_clust_cpcs_q4 = e(N_clust)
matrix cpcs_q4 = r(table)

// results
foreach i in cog rses cpcs{
foreach j in 1 2 3 4 {
* beta
scalar `i'_q`j'_est = `i'_q`j'[1,1]
* standard error
* scalar `i'_q`j'_se = `i'_q`j'[2,1]
* p value
scalar `i'_q`j'_pv = `i'_q`j'[3,1]

// significant level
if `i'_q`j'_pv <= 0.01 {
local star_`i'_q`j' %3s "***"
}
else if (`i'_q`j'_pv >0.01) & (`i'_q`j'_pv <=0.05) {
local star_`i'_q`j' %2s "**"
}
else if (`i'_q`j'_pv >0.05) & (`i'_q`j'_pv <=0.10) {
local star_`i'_q`j' %1s "*"
}
else {
local star_`i'_q`j'  ""
}
}
}





/// Table
tempname hh2
file open `hh2' using "$path_output/hetero_quartile.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Heterogeneity among Baseline Abilites (Quantile)}" _newline
file write `hh2' "\label{tab:hetero_quantile}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccccccc}\toprule" _newline

  
file write `hh2' " Dependent Variable & Baseline  & Difference & Obs & N of clusters  \\\midrule\midrule" _newline
file write `hh2' " Rapid math test score^{a} & Math quartile 4  & " %04.3f (cog_q4_est) `star_cog_q4' " & " %02.0f (n_cog_q4) " & " %02.0f ( n_clust_cog_q4 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cog_q4_pv) " ) & &  \\ " _newline
file write `hh2' "  & Math quartile 3  & " %04.3f (cog_q3_est) `star_cog_q3' " & " %02.0f (n_cog_q3) " & " %02.0f ( n_clust_cog_q3 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cog_q3_pv) " ) & &  \\ " _newline
file write `hh2' "  & Math quartile 2  & " %04.3f (cog_q2_est) `star_cog_q2' " & " %02.0f (n_cog_q2) " & " %02.0f ( n_clust_cog_q2 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cog_q2_pv) " ) & &  \\ " _newline
file write `hh2' "  & Math quartile 1  & " %04.3f (cog_q1_est) `star_cog_q1' " & " %02.0f (n_cog_q1) " & " %02.0f ( n_clust_cog_q1 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cog_q1_pv) " ) & &  \\ " _newline

file write `hh2' " RSES^{a} & RSES quartile 4  & " %04.3f (rses_q4_est) `star_rses_q4' " & " %02.0f (n_rses_q4) " & " %02.0f ( n_clust_rses_q4 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (rses_q4_pv) " ) & &  \\ " _newline
file write `hh2' "  & RSES quartile 3  & " %04.3f (rses_q3_est) `star_rses_q3' " & " %02.0f (n_rses_q3) " & " %02.0f ( n_clust_rses_q3 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (rses_q3_pv) " ) & &  \\ " _newline
file write `hh2' "  & RSES quartile 2  & " %04.3f (rses_q2_est) `star_rses_q2' " & " %02.0f (n_rses_q2) " & " %02.0f ( n_clust_rses_q2 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (rses_q2_pv) " ) & &  \\ " _newline
file write `hh2' "  & RSES quartile 1  & " %04.3f (rses_q1_est) `star_rses_q1' " & " %02.0f (n_rses_q1) " & " %02.0f ( n_clust_rses_q1 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (rses_q1_pv) " ) & &  \\ " _newline

file write `hh2' " CPCS^{a} & CPCS quartile 4  & " %04.3f (cpcs_q4_est) `star_cpcs_q4' " & " %02.0f (n_cpcs_q4) " & " %02.0f ( n_clust_cpcs_q4 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cpcs_q4_pv) " ) & &  \\ " _newline
file write `hh2' "  & CPCS quartile 3  & " %04.3f (cpcs_q3_est) `star_cpcs_q3' " & " %02.0f (n_cpcs_q3) " & " %02.0f ( n_clust_cpcs_q3 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cpcs_q3_pv) " ) & &  \\ " _newline
file write `hh2' "  & CPCS quartile 2  & " %04.3f (cpcs_q2_est) `star_cpcs_q2' " & " %02.0f (n_cpcs_q2) " & " %02.0f ( n_clust_cpcs_q2 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cpcs_q2_pv) " ) & &  \\ " _newline
file write `hh2' "  & CPCS quartile 1  & " %04.3f (cpcs_q1_est) `star_cpcs_q1' " & " %02.0f (n_cpcs_q1) " & " %02.0f ( n_clust_cpcs_q1 ) " \\ " _newline
file write `hh2' "    			&	 & ( " %04.3f (cpcs_q1_pv) " ) & &  \\ " _newline

file write `hh2' "\\\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Dependent variables are standardized using the average and variance of the whole follow-up sample in the February 2022 survey. " _newline
file write `hh2' "\item (b) Cutoffs are created based on their ability to perform each item at the baseline. " _newline
file write `hh2' "\item (c) Wild cluster bootstrap p-values are reported within parentheses. Clusters are schools at the baseline." _newline
file write `hh2' "\item (d) $^*$ Significant at 10\% level; $^{**}$ significant at 5\% level; $^{***}$ significant at 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
